package com.itboye.textileexpes.adapter;

import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.BaseInfo;
import com.itboye.textileexpes.beans.GroupInfo;
import com.itboye.textileexpes.beans.ProductInfo;
import com.itboye.textileexpres.R;

/**
 * ExpandableListView的购物车适配器
 * 
 * @author chenjunsen 2015年8月18日下午8:53:57
 */
public class ShopcartExpandableListViewAdapter extends
		BaseExpandableListAdapter implements OnClickListener {
	private List<GroupInfo> groups;
	private Map<String, List<ProductInfo>> children;
	private Context context;
	ChildHolder cholder;

	private CheckInterface checkInterface;
	private ModifyCountInterface modifyCountInterface;

	/**
	 * 构造函数
	 * 
	 * @param groups
	 *            组元素列表
	 * @param children
	 *            子元素列表
	 * @param context
	 */
	public ShopcartExpandableListViewAdapter(List<GroupInfo> groups,
			Map<String, List<ProductInfo>> children, Context context) {
		super();
		this.groups = groups;
		this.children = children;
		this.context = context;
	}

	public void setCheckInterface(CheckInterface checkInterface) {
		this.checkInterface = checkInterface;
	}

	public void setModifyCountInterface(
			ModifyCountInterface modifyCountInterface) {
		this.modifyCountInterface = modifyCountInterface;
	}

	@Override
	public int getGroupCount() {
		return groups.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		String groupId = groups.get(groupPosition).getId();
		return children.get(groupId).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		List<ProductInfo> childs = children.get(groups.get(groupPosition)
				.getId());
		return childs.get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupHolder gholder;
		if (convertView == null) {
			gholder = new GroupHolder();
			convertView = View.inflate(context,
					R.layout.item__shop_shopcart_group, null);
			gholder.cb_check = (CheckBox) convertView
					.findViewById(R.id.cb_check);
			gholder.tv_group_name = (TextView) convertView
					.findViewById(R.id.tv_group_name);
			gholder.btnMesge = (Button) convertView.findViewById(R.id.btnMesge);
			convertView.setTag(gholder);
		} else {
			gholder = (GroupHolder) convertView.getTag();
		}

		final GroupInfo group = (GroupInfo) getGroup(groupPosition);
		if (group != null) {
			gholder.tv_group_name.setText(group.getName());
			gholder.cb_check.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					group.setChoosed(((CheckBox) v).isChecked());
					checkInterface.checkGroup(groupPosition,
							((CheckBox) v).isChecked());// 暴露组选接口
				}
			});

			gholder.btnMesge.setTag(groupPosition);
			gholder.btnMesge.setOnClickListener(new OnClickListener() {

				@SuppressLint("NewApi")
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub LineGroup
					// group.setEditing(((LinearLayout) v).isLaidOut());
					// checkInterface.LineGroup(groupPosition,
					// ((CheckBox) v).isChecked());// 暴露组选接口
				}
			});
			gholder.cb_check.setChecked(group.isChoosed());

		}
		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		if (convertView == null) {
			cholder = new ChildHolder();
			convertView = View.inflate(context,
					R.layout.item_shop_shopcart_product, null);
			cholder.cb_check = (CheckBox) convertView
					.findViewById(R.id.cb_check);
			cholder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
			cholder.tv_product_name = (TextView) convertView
					.findViewById(R.id.tv_product_name);
			cholder.tv_product_desc = (TextView) convertView
					.findViewById(R.id.tv_product_desc);
			cholder.tv_price = (TextView) convertView
					.findViewById(R.id.tv_price);
			cholder.iv_increase = (ImageView) convertView
					.findViewById(R.id.iv_increase);
			cholder.iv_decrease = (ImageView) convertView
					.findViewById(R.id.iv_decrease);
			cholder.tv_count = (TextView) convertView
					.findViewById(R.id.tv_buy_count);
			cholder.linglayoute = (LinearLayout) convertView
					.findViewById(R.id.linglayoute);
			cholder.linglayout = (LinearLayout) convertView
					.findViewById(R.id.linglayout);
			convertView.setTag(cholder);
		} else {
			cholder = (ChildHolder) convertView.getTag();
		}
		final ProductInfo product = (ProductInfo) getChild(groupPosition,
				childPosition);
		if (product != null) {
			cholder.tv_product_name.setText(product.getName());
			cholder.tv_product_desc.setText(product.getDesc());
			cholder.tv_price.setText("￥" + product.getPrice() + "");
			cholder.tv_count.setText(product.getCount() + "");
			cholder.cb_check.setChecked(product.isChoosed());
			cholder.cb_check.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					product.setChoosed(((CheckBox) v).isChecked());
					cholder.cb_check.setChecked(((CheckBox) v).isChecked());
					checkInterface.checkChild(groupPosition, childPosition,
							((CheckBox) v).isChecked());// 暴露子选接口
				}
			});
			cholder.iv_increase.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					modifyCountInterface.doIncrease(groupPosition,
							childPosition, cholder.tv_count,
							cholder.cb_check.isChecked());// 暴露增加接口
				}
			});
			cholder.iv_decrease.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					modifyCountInterface.doDecrease(groupPosition,
							childPosition, cholder.tv_count,
							cholder.cb_check.isChecked());// 暴露删减接口
				}
			});
		}
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	/***
	 * 点击
	 */
	View.OnClickListener listener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			boolean isEting = false;
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnMesge:
				cholder.linglayoute.setVisibility(View.VISIBLE);
				cholder.linglayoute.setVisibility(View.GONE);

				// int groupPosition2 = Integer
				// .parseInt(String.valueOf(v.getTag()));
				// boolean isEditing = !(groups.get(groupPosition2).getInfo()
				// .get(groupPosition2).isEditing());
				// groups.get(groupPosition2).getInfo().get(groupPosition2)
				// .setEditing(isEditing);
				// for (int i = 0; i <
				// groups.get(groupPosition2).getInfo().size(); i++) {
				// groups.get(groupPosition2).getInfo().get(groupPosition2)
				// .setEditing(isEditing);
				// // }
				notifyDataSetChanged();
				break;

			default:
				break;
			}
		}
	};

	/**
	 * 组元素绑定器
	 * 
	 * @author chenjunsen 2015年8月18日下午8:56:15
	 */
	private class GroupHolder {
		CheckBox cb_check;
		TextView tv_group_name;
		Button btnMesge;
	}

	/**
	 * 子元素绑定器
	 * 
	 * @author chenjunsen 2015年8月18日下午8:56:25
	 */
	private class ChildHolder {
		CheckBox cb_check;
		ImageView iv_pic;
		TextView tv_product_name;
		TextView tv_product_desc;
		TextView tv_price;
		ImageView iv_increase;
		TextView tv_count;
		ImageView iv_decrease;
		LinearLayout linglayoute, linglayout;
	}

	/**
	 * 复选框接口
	 * 
	 * @author chenjunsen 2015年8月18日下午8:56:39
	 */
	public interface CheckInterface {
		/**
		 * 组选框状态改变触发的事件
		 * 
		 * @param groupPosition
		 *            组元素位置
		 * @param isChecked
		 *            组元素选中与否
		 */
		public void checkGroup(int groupPosition, boolean isChecked);

		public void LineGroup(int groupPosition, boolean isChecked);

		/**
		 * 子选框状态改变时触发的事件
		 * 
		 * @param groupPosition
		 *            组元素位置
		 * @param childPosition
		 *            子元素位置
		 * @param isChecked
		 *            子元素选中与否
		 */
		public void checkChild(int groupPosition, int childPosition,
				boolean isChecked);
	}

	/**
	 * 改变数量的接口
	 * 
	 * @author chenjunsen 2015年8月18日下午8:56:50
	 */
	public interface ModifyCountInterface {
		/**
		 * 增加操作
		 * 
		 * @param groupPosition
		 *            组元素位置
		 * @param childPosition
		 *            子元素位置
		 * @param showCountView
		 *            用于展示变化后数量的View
		 * @param isChecked
		 *            子元素选中与否
		 */
		public void doIncrease(int groupPosition, int childPosition,
				View showCountView, boolean isChecked);

		/**
		 * 删减操作
		 * 
		 * @param groupPosition
		 *            组元素位置
		 * @param childPosition
		 *            子元素位置
		 * @param showCountView
		 *            用于展示变化后数量的View
		 * @param isChecked
		 *            子元素选中与否
		 */
		public void doDecrease(int groupPosition, int childPosition,
				View showCountView, boolean isChecked);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
